'use strict';

module.exports = (app) => {
  class CarouselGroupController extends app.Controller {
    /**
         *
         * @api {POST} /api/carouselgroup/create 创建轮播图分组
         * @apiName 轮播图分组
         * @apiGroup 轮播图
         * @apiVersion  1.0.0
         *
         * 
         * @apiParam  {STRING} site 轮播图位置
         *
         *
         * @apiParamExample  {type} Request-Example:
         * {
         *     project_id: 1,
         *     site: "首页-底部"
         * }
         *
         *
         * @apiSuccessExample {type} Success-Response:
            {
                "msg": "成功操作",
                "result": {
                    "id": 3,
                    "site": "首页底部",
                    "project_id": 1
                }
            }
         *
         *
         */
    async create() {
      const { app, ctx, service } = this;

      /** 验证数据格式是否合法 */
      const checkRes = await new ctx.helper.checkData(this, 'site');
      if (!checkRes.checkDataRes) {
        return;
      }

      const createRes = await service.shop.carouselGroup.create();
      if (createRes.dataValues) {
        ctx.body = ctx.response.success(createRes.dataValues);
      } else {
        ctx.body = ctx.response.error(createRes);
      }
    }

    async delete() {
      const { app, ctx, service } = this;
      const data = ctx.request.body;
      let checkRes = ctx.helper.dataCheck('id');
      //如果有缺少参数
      if (checkRes) {
        return ctx.helper.lackModule(checkRes);
      }
      try {
        let res = await service.shop.carouselGroup.delete();
        return ctx.helper.successModule(res);
      } catch (error) {
        console.log(error);
        return ctx.helper.errorModule('删除失败');
      }
    }

    async get() {
      const { app, ctx, service } = this;

      const getRes = await service.shop.carouselGroup.get();

      if (getRes.length === 0 || getRes[0].dataValues) {
        ctx.body = ctx.response.success(getRes);
      } else {
        ctx.body = ctx.response.error(getRes);
      }
    }

    async show() {
      const { ctx, service } = this;
      const id = ctx.params.id;

      if (!id) return ctx.helper.lackModule('id');

      const showRes = await service.shop.carouselGroup.show(id);
      if (showRes) {
        ctx.body = ctx.response.success(showRes);
      } else {
        ctx.body = ctx.response.error('该分组不存在');
      }
    }
  }
  return CarouselGroupController;
};
